package com.tanhua.dubbo.api;

import com.tanhua.domain.mongo.FollowUser;
import com.tanhua.domain.mongo.Video;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.util.IdSequeue;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

/**
 * @author anshuai
 * @create 2021-02-02 15:34
 */
@Service
public class VideoApiImpl implements VideoApi {

    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private IdSequeue idSequeue;

    /**
     * 保存小视频信息
     *
     * @param video 小视频信息对象
     */
    @Override
    public String save(Video video) {

        video.setId(ObjectId.get());
        video.setCreated(System.currentTimeMillis());
        //因为mongodb单独保存key-value形式的数据,所以自增id字段只能以文档(集合)形式保存,所以随便给一个key做标识即可
        video.setVid(idSequeue.getNextId("video"));

        mongoTemplate.save(video);

        return video.getId().toHexString();
    }

    /**
     * 分页查询小视频列表
     *
     * @param page     页码
     * @param pagesize 查询几条
     * @param userId   给谁推荐的
     * @return
     */
    @Override
    public PageResult<Video> findVideoList(int page, int pagesize, long userId) {
        Query query = new Query()
                .with(Sort.by(Sort.Order.desc("created")))
                .with(PageRequest.of(page - 1, pagesize));

        List<Video> videoList = mongoTemplate.find(query, Video.class);
        //计算总数量
        long count = mongoTemplate.count(query, Video.class);

        //计算总页数
        int pages = (int) Math.ceil(count * 1.0 / pagesize);

        return new PageResult<>((int) count, pagesize, pages, page, videoList);
    }

    /**
     * 关注用户
     *
     * @param userId       用户id
     * @param followUserId 关注了谁
     */
    @Override
    public void followUser(Long userId, Long followUserId) {

        FollowUser followUser = new FollowUser();
        followUser.setCreated(System.currentTimeMillis());
        followUser.setId(ObjectId.get());
        followUser.setUserId(userId);
        followUser.setFollowUserId(followUserId);

        mongoTemplate.save(followUser);
    }

    /**
     * 取消关注
     *
     * @param userId       用户id
     * @param followUserId 取关了谁
     */
    @Override
    public void disFollowUser(long userId, long followUserId) {
        Criteria criteria = Criteria.where("userId").is(userId).and("followUserId").is(followUserId);
        mongoTemplate.remove(new Query(criteria), FollowUser.class);
    }

    /**
     * 查询一条视频信息
     *
     * @param videoId 视频id
     * @return
     */
    @Override
    public Video findById(String videoId) {
        return mongoTemplate.findById(new ObjectId(videoId), Video.class);
    }

    /**
     * 分页查询某一用户发布的视频列表
     *
     * @param page     页码
     * @param pagesize 查询几条
     * @param uid      用户id
     * @return
     */
    @Override
    public PageResult<Video> findVideoListByUid(int page, int pagesize, Long uid) {
        Query query = new Query()
                .with(Sort.by(Sort.Order.desc("created")))
                .with(PageRequest.of(page - 1, pagesize));

        if (uid != null) {
            query.addCriteria(Criteria.where("userId").is(uid));
        }

        List<Video> videoList = mongoTemplate.find(query, Video.class);
        long count = mongoTemplate.count(query, Video.class);

        int pages = (int) Math.ceil(count * 1.0 / pagesize);

        return new PageResult<>((int) count,pagesize,pages,page,videoList);
    }

    /**
     * 根据大数据推荐字段vid查询视频列表
     * @param vidList vid集合
     * @return
     */
    @Override
    public List<Video> findVideoByVids(List<Long> vidList) {
        Query query = new Query(Criteria.where("vid").in(vidList));
        return mongoTemplate.find(query,Video.class);
    }
}
